package com.shine.app.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.shine.app.AppException;
import com.shine.app.AppManager;
import com.shine.app.R;
import com.shine.app.adapter.LocationAdapter;
import com.shine.app.adapter.ViewPagerAdapter;
import com.shine.app.bean.PlaceNearBean;
import com.shine.app.bean.request.LocationNearBean;
import com.shine.app.commons.Commons;
import com.shine.app.commons.StringUtils;

public class LocationSearchActivity extends LocationBaseActivity implements
		OnClickListener {

	private String country;
	private SearchAsyncTask searchAsyncTask;
	private LocationAdapter adapter = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.viewpager_location);
		InitViewPagerView();
		InitPagerImageView();
		InitViewPager();
		init();
		initRecentsCount();
		clickListener();
	}

	protected void clickListener() {
		nearListView.setOnItemClickListener(this);
		searchLayout.setOnClickListener(this);
		searchImageView.setOnClickListener(this);
		gobackImageView.setOnClickListener(this);
		headNameTextView.setOnClickListener(this);
		autoCompleteTextView.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (StringUtils.isEmpty(s.toString())) {
					progressBar_near.setVisibility(View.GONE);
					adapter.setData(new ArrayList<PlaceNearBean>());
					adapter.notifyDataSetChanged();
					return;
				}
				country = s.toString();
				getData();
			}
		});
	}

	private void getData() {
		if (searchAsyncTask != null
				&& searchAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
			searchAsyncTask.cancel(true);
		}
		LocationNearBean locationNearBean = getRequestdata();
		searchAsyncTask = new SearchAsyncTask();
		searchAsyncTask.execute(locationNearBean);
	}

	public class SearchAsyncTask extends
			AsyncTask<Object, Integer, List<PlaceNearBean>> {
		private LocationNearBean locationNearBean;

		@Override
		protected List<PlaceNearBean> doInBackground(Object... params) {
			List<PlaceNearBean> list = null;
			try {
				locationNearBean = (LocationNearBean) params[0];
				list = appContext.locationByName(locationNearBean);
			} catch (AppException e) {
				e.printStackTrace();
			}
			if (list == null) {
				list = new ArrayList<PlaceNearBean>();
			}
			return list;
		}

		@Override
		protected void onPostExecute(List<PlaceNearBean> result) {
			super.onPostExecute(result);
			adapter.setData(result);
			adapter.notifyDataSetChanged();
			progressBar_near.setVisibility(View.GONE);
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressBar_near.setVisibility(View.VISIBLE);
		}
	}

	private LocationNearBean getRequestdata() {
		LocationNearBean locationNearBean = new LocationNearBean();
		locationNearBean.setCount(Commons.PAGE_SIZE);
		locationNearBean.setName(country);
		locationNearBean.setType(Commons.LISTVIEW_TAB_ZERO);
		locationNearBean.setLatitude(0);
		locationNearBean.setLongitude(0);
		return locationNearBean;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.head_seacher:
			if ((Boolean) searchImageView.getTag()) {
				searchImageView.setImageResource(R.drawable.icon_clear);
				autoCompleteTextView.setVisibility(View.VISIBLE);
				headNameTextView.setVisibility(View.INVISIBLE);
				searchImageView.setTag(false);
			} else {
				inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(),
						0);
				searchImageView.setImageResource(R.drawable.head_search);
				autoCompleteTextView.getText().clear();
				adapter.setData(new ArrayList<PlaceNearBean>());
				adapter.notifyDataSetChanged();
				autoCompleteTextView.setVisibility(View.INVISIBLE);
				headNameTextView.setVisibility(View.VISIBLE);
				progressBar_near.setVisibility(View.GONE);
				searchImageView.setTag(true);
			}
			break;

		case R.id.select_near:
			Intent intent = new Intent(this, LocationResultActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			intent.putExtra("searchBean", searchBean);
			this.startActivity(intent);
			AppManager.getAppManager().finishActivity();
			overridePendingTransition(R.anim.slide_in_up, R.anim.anim);
			break;

		case R.id.head_back:
		case R.id.head_name:
			AppManager.getAppManager().finishActivity();
			overridePendingTransition(R.anim.anim, R.anim.slide_out_down);
			break;

		default:
			break;
		}
	}

	protected void init() {
		initHeadView();
		searchLayout = (LinearLayout) pageOne.findViewById(R.id.select_near);
		nearListView = (ListView) pageOne.findViewById(R.id.listview);
		progressBar_near = (ProgressBar) pageOne.findViewById(R.id.progressBar);
		List<PlaceNearBean> list = new ArrayList<PlaceNearBean>();
		adapter = new LocationAdapter(LocationSearchActivity.this, list);
		nearListView.setAdapter(adapter);

		searchImageView.setImageResource(R.drawable.head_search);
		searchImageView.setTag(true);
		autoCompleteTextView.setVisibility(View.INVISIBLE);
		progressBar_near.setVisibility(View.GONE);
	}

	protected void InitViewPager() {
		pagerList = new ArrayList<View>();
		pageOne = inflater.inflate(R.layout.location_nearme, null);
		pagerList.add(pageOne);
		viewPager.setAdapter(new ViewPagerAdapter(pagerList));
		viewPager.setCurrentItem(0);
	}

	@Override
	protected void onDestroy() {
		if (searchAsyncTask != null
				&& searchAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
			searchAsyncTask.cancel(true);
		}
		super.onDestroy();
	}

}
